//
// File: trapz.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 23-Apr-2025 16:26:18
//

// Include Files
#include "trapz.h"
#include "rt_nonfinite.h"
#include "coder_array.h"
#include <cmath>
#include <string.h>

// Function Definitions
//
// Arguments    : const ::coder::array<double, 2U> &x
//                const ::coder::array<double, 2U> &y
// Return Type  : double
//
namespace coder {
double trapz(const ::coder::array<double, 2U> &x,
             const ::coder::array<double, 2U> &y)
{
  array<double, 1U> c;
  double z;
  z = 0.0;
  if (y.size(1) <= 1) {
    if ((x.size(1) == 1) && (std::isinf(x[0]) || std::isnan(x[0]))) {
      z = rtNaN;
    }
  } else {
    int i;
    int ix;
    if (x.size(1) == 1) {
      c.set_size(y.size(1));
      ix = y.size(1);
      for (i = 0; i < ix; i++) {
        c[i] = x[0];
      }
      double c1;
      c1 = 0.5 * x[0];
      c[0] = c1;
      c[y.size(1) - 1] = c1;
    } else {
      c.set_size(y.size(1));
      c[0] = 0.5 * (x[1] - x[0]);
      i = y.size(1) - 1;
      for (ix = 2; ix <= i; ix++) {
        c[ix - 1] = 0.5 * (x[ix] - x[ix - 2]);
      }
      c[y.size(1) - 1] = 0.5 * (x[y.size(1) - 1] - x[y.size(1) - 2]);
    }
    ix = 0;
    i = y.size(1);
    for (int iac{1}; iac <= i; iac++) {
      for (int ia{iac}; ia <= iac; ia++) {
        z += y[ia - 1] * c[ix];
      }
      ix++;
    }
  }
  return z;
}

//
// Arguments    : const ::coder::array<double, 1U> &x
//                const ::coder::array<double, 1U> &y
// Return Type  : double
//
double trapz(const ::coder::array<double, 1U> &x,
             const ::coder::array<double, 1U> &y)
{
  array<double, 1U> c;
  double z;
  z = 0.0;
  if (y.size(0) <= 1) {
    if ((x.size(0) == 1) && (std::isinf(x[0]) || std::isnan(x[0]))) {
      z = rtNaN;
    }
  } else {
    double c1;
    int iac;
    int vlen;
    if (x.size(0) == 1) {
      c.set_size(y.size(0));
      vlen = y.size(0);
      for (iac = 0; iac < vlen; iac++) {
        c[iac] = x[0];
      }
      c1 = 0.5 * x[0];
      c[0] = c1;
      c[y.size(0) - 1] = c1;
    } else {
      c.set_size(y.size(0));
      c[0] = 0.5 * (x[1] - x[0]);
      iac = y.size(0) - 1;
      for (vlen = 2; vlen <= iac; vlen++) {
        c[vlen - 1] = 0.5 * (x[vlen] - x[vlen - 2]);
      }
      c[y.size(0) - 1] = 0.5 * (x[y.size(0) - 1] - x[y.size(0) - 2]);
    }
    vlen = y.size(0);
    for (iac = 1; vlen < 0 ? iac >= 1 : iac <= 1; iac += vlen) {
      c1 = 0.0;
      for (int ia{1}; ia <= vlen; ia++) {
        c1 += y[ia - 1] * c[ia - 1];
      }
      z += c1;
    }
  }
  return z;
}

//
// Arguments    : const double x_data[]
//                const int x_size[2]
//                const double y_data[]
//                int y_size
// Return Type  : double
//
double trapz(const double x_data[], const int x_size[2], const double y_data[],
             int y_size)
{
  array<double, 1U> c;
  double z;
  z = 0.0;
  if (y_size <= 1) {
    if ((x_size[1] == 1) && (std::isinf(x_data[0]) || std::isnan(x_data[0]))) {
      z = rtNaN;
    }
  } else {
    double c1;
    int iac;
    if (x_size[1] == 1) {
      c.set_size(y_size);
      for (iac = 0; iac < y_size; iac++) {
        c[iac] = x_data[0];
      }
      c1 = 0.5 * x_data[0];
      c[0] = c1;
      c[y_size - 1] = c1;
    } else {
      c.set_size(y_size);
      c[0] = 0.5 * (x_data[1] - x_data[0]);
      iac = y_size - 1;
      for (int k{2}; k <= iac; k++) {
        c[k - 1] = 0.5 * (x_data[k] - x_data[k - 2]);
      }
      c[y_size - 1] = 0.5 * (x_data[y_size - 1] - x_data[y_size - 2]);
    }
    for (iac = 1; y_size < 0 ? iac >= 1 : iac <= 1; iac += y_size) {
      c1 = 0.0;
      for (int k{1}; k <= y_size; k++) {
        c1 += y_data[k - 1] * c[k - 1];
      }
      z += c1;
    }
  }
  return z;
}

} // namespace coder

//
// File trailer for trapz.cpp
//
// [EOF]
//
